package com.lcredu.seckill.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 黎长容
 * @date 2025-08-26 18:27
 */
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private UserArgumentResolver userArgumentResolver;

    //注册拦截器
    @Resource
    private AccessLimitInterceptor accessLimitInterceptor;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(accessLimitInterceptor);
    }

    /**
     * 添加静态资源处理器
     * 配置静态资源的访问路径和位置
     *
     * @param registry 资源处理器注册器，用于注册静态资源处理规则
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置所有路径的静态资源映射到classpath下的static目录
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }

    /**
     * 添加自定义方法参数解析器
     * 将自定义的用户参数解析器添加到解析器列表中
     *
     * @param resolvers 方法参数解析器列表，用于注册自定义参数解析器
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        // 添加用户自定义参数解析器
        resolvers.add(userArgumentResolver);
    }


}
